﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.DAL
Imports Exp.Model

Public Class User_RequestJoin_Service
    Private _objRqJoin As New RET_User_ReqeuestJoin


    Public Sub New(objRqJoin As RET_User_ReqeuestJoin)
        _objRqJoin = objRqJoin
    End Sub

    Public Sub New()

    End Sub

    Public Function GetAll() As List(Of RET_User_ReqeuestJoin)
        Dim ctx As New EXPEntities
        Dim list = ctx.RET_User_ReqeuestJoin.ToList()
        Return list
    End Function

    Public Function GetByUserID(ByVal User_ID As Integer) As RET_User_ReqeuestJoin
        Dim ctx As New EXPEntities
        Dim listObj = ctx.RET_User_ReqeuestJoin.Where(Function(o) o.Requester_ID = User_ID).ToList
        If listObj.Count > 0 Then
            Return listObj.OrderByDescending(Function(o) o.ReqJoin_ID).First
        Else
            Return Nothing
        End If

    End Function

 
    Public Sub Add()
        Dim ctx As New EXPEntities
        ctx.RET_User_ReqeuestJoin.AddObject(_objRqJoin)
        ctx.SaveChanges()
    End Sub

    Public Sub Approve(ByVal isApprove As Boolean)
        Try
            Dim ctx As New EXPEntities
            Dim obj = ctx.RET_User_ReqeuestJoin.Where(Function(o) o.ReqJoin_ID = _objRqJoin.ReqJoin_ID).FirstOrDefault()
            obj.Status = _objRqJoin.Status
            obj.UpdateDate = Now
            obj.UpdateUser = _objRqJoin.UpdateUser

            If isApprove = True Then
                obj.Status = "APPROVE"
            Else
                obj.Status = "REJECT"
                obj.ReasonReject = _objRqJoin.ReasonReject
            End If

            ctx.SaveChanges()
        Catch ex As Exception

        End Try
    End Sub

    'Public Sub UpdateLogin(ByVal User_ID As Integer)
    '    Dim ctx As New EXPEntities
    '    Dim obj = ctx.REM_User.Where(Function(o) o.User_ID = User_ID).FirstOrDefault()
    '    obj.LastLoginDate = Now
    '    ctx.SaveChanges()
    'End Sub

    'Public Sub Delete(ByVal ID As Integer)
    '    Dim ctx As New EXPEntities
    '    Dim obj = ctx.REM_User.Where(Function(o) o.User_ID = ID).FirstOrDefault()
    '    ctx.REM_User.DeleteObject(obj)
    '    ctx.SaveChanges()
    'End Sub

    'Public Function isHasRight(ByVal User_ID As Integer, ByVal MenuName As String) As Boolean
    '    Dim Result As Boolean = False
    '    Dim ctx As New EXPEntities
    '    Dim RoleID = ctx.REM_User.Where(Function(o) o.User_ID = User_ID).FirstOrDefault.Role_ID
    '    Dim iCount = ctx.REM_RoleMenu.Where(Function(o) o.Role_ID = RoleID And o.MenuName = MenuName).Count
    '    If iCount = 1 Then Result = True
    '    Return Result

    'End Function

    'Public Function getOwnerDetail(ByVal UserId As Integer) As List(Of EXP_Owner_detail)
    '    Dim ctx As New EXPEntities
    '    Return ctx.EXP_Get_Owner_detail(UserId).ToList()
    'End Function

    'Public Function GetListApprover() As List(Of REM_User)
    '    Dim ctx As New EXPEntities
    '    Dim list = ctx.REM_User.Where(Function(o) o.Role_ID = 2).OrderBy(Function(o) o.Email).ToList

    '    Return list
    'End Function

    Public Function Search(ByVal RequestID As Integer?, ByVal keyword As String, ByVal JoinDate As DateTime?, ByRef ExpireDate As DateTime?, ByVal Status As String) As List(Of EXP_Get_Join_Request_Result)
        Dim ctx As New EXPEntities
        Return ctx.EXP_Get_Join_Request(RequestID, keyword, JoinDate, ExpireDate, Status).ToList()
    End Function

    Public Sub UpdateStatus(ByVal Updator As Integer, ByVal REQ_ID As Integer, ByVal Status As String, ByVal Reason As String, ByVal JoinDate As DateTime)
        Dim ctx As New EXPEntities
        Dim r As RET_User_ReqeuestJoin = ctx.RET_User_ReqeuestJoin.Where(Function(o) o.ReqJoin_ID = REQ_ID).FirstOrDefault()

        If r IsNot Nothing Then
            r.Status = Status
            r.ReasonReject = Reason
            r.UpdateDate = Now
            r.UpdateUser = Updator

            r.JoinDate = JoinDate
            r.ExpireDate = JoinDate.AddDays(365)

            r.REM_User.JoinDate = r.JoinDate
            r.REM_User.ExpireDate = r.ExpireDate

            ctx.SaveChanges()
        End If

    End Sub

End Class
